package edu.qd.learn.newchapter.chapter_thread.day01;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.StampedLock;

/**
 * lock锁
 *
 * @author 布衣
 */
public class FourThreadLock extends Thread {

//    private final StampedLock sl = new StampedLock();
//
//    public void run(){
//        long l = sl.tryWriteLock();
//
//        try{
//
//        }finally {
//            sl.unlock(l);
//        }
//    }


    private final Lock lock = new ReentrantLock();

    /**
     * lock();上锁
     * tryLock() 尝试获取锁，获取不到返回false
     * lock.tryLock(3000, TimeUnit.MILLISECONDS) 尝试获取锁，获取不到等待3000毫秒
     */
    @Override
    public void run() {
        try {
            if (lock.tryLock(3000, TimeUnit.MILLISECONDS)) {
                try {

                    //代码块
                } finally {
                    lock.unlock();
                }
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

    }
}
